﻿using AutoMapper;
using Compass.Wasm.Shared;
using Compass.Wasm.Shared.Wms;
using Compass.WmsService.Domain;
using Compass.WmsService.Domain.Entities;
using Compass.WmsService.Infrastructure;

namespace Compass.Wasm.Server.Services.Wms;
public interface IVendorService:IBaseService<VendorDto>
{
    Task<ApiResponse<VendorDto>> GetSingleByNumberAsync(string number);
    Task<ApiResponse<IEnumerable<VendorDto>>> GetTop5ByKeywordAsync(string? keyword);
}
public class VendorService:IVendorService
{
    #region ctor
    private readonly WmsDbContext _dbContext;
    private readonly IWmsRepository _repository;
    private readonly IMapper _mapper;
    public VendorService(WmsDbContext dbContext, IWmsRepository repository, IMapper mapper)
    {
        _dbContext = dbContext;
        _repository = repository;
        _mapper = mapper;
    }
    #endregion

    #region 基本增删改查
    public async Task<ApiResponse<IEnumerable<VendorDto>>> GetAllAsync()
    {
        try
        {
            var models = await _repository.GetVendorsAsync();
            var dtos = await _mapper.ProjectTo<VendorDto>(models).OrderBy(x => x.Number).ToListAsync();
            return new ApiResponse<IEnumerable<VendorDto>> { Status = true, Result = dtos };
        }
        catch (Exception e)
        {
            return new ApiResponse<IEnumerable<VendorDto>> { Status = false, Message = e.Message };
        }
    }

    public async Task<ApiResponse<VendorDto>> GetSingleAsync(Guid id)
    {
        try
        {
            var model = await _repository.GetVendorByIdAsync(id);
            if (model == null) return new ApiResponse<VendorDto> { Status = false, Message = "查询数据失败" };
            var dto = _mapper.Map<VendorDto>(model);
            return new ApiResponse<VendorDto> { Status = true, Result = dto };
        }
        catch (Exception e)
        {
            return new ApiResponse<VendorDto> { Status = false, Message = e.Message };
        }
    }

    public async Task<ApiResponse<VendorDto>> AddAsync(VendorDto dto)
    {
        try
        {
            var model = new Vendor(Guid.NewGuid(), dto.Number, dto.Name, dto.Contact,
                dto.Address, dto.Telephone,  dto.Remarks);
            await _dbContext.Vendors.AddAsync(model);
            dto.Id = model.Id;
            return new ApiResponse<VendorDto> { Status = true, Result = dto };
        }
        catch (Exception e)
        {
            return new ApiResponse<VendorDto> { Status = false, Message = e.Message };
        }
    }

    public async Task<ApiResponse<VendorDto>> UpdateAsync(Guid id, VendorDto dto)
    {
        try
        {
            var model = await _repository.GetVendorByIdAsync(id);
            if (model == null) return new ApiResponse<VendorDto> { Status = false, Message = "更新数据失败" };
            model.Update(dto);
            return new ApiResponse<VendorDto> { Status = true, Result = dto };
        }
        catch (Exception e)
        {
            return new ApiResponse<VendorDto> { Status = false, Message = e.Message };
        }
    }

    public async Task<ApiResponse<VendorDto>> DeleteAsync(Guid id)
    {
        try
        {
            var model = await _repository.GetVendorByIdAsync(id);
            if (model == null) return new ApiResponse<VendorDto> { Status = false, Message = "删除数据失败" };
            model.SoftDelete();//软删除
            return new ApiResponse<VendorDto> { Status = true };
        }
        catch (Exception e)
        {
            return new ApiResponse<VendorDto> { Status = false, Message = e.Message };
        }
    }


    #endregion

    #region 扩展查询
    public async Task<ApiResponse<VendorDto>> GetSingleByNumberAsync(string number)
    {
        try
        {
            var model = await _repository.GetVendorByNumberAsync(number);
            if (model == null) return new ApiResponse<VendorDto> { Status = false, Message = "查询数据失败" };
            var dto = _mapper.Map<VendorDto>(model);
            return new ApiResponse<VendorDto> { Status = true, Result = dto };
        }
        catch (Exception e)
        {
            return new ApiResponse<VendorDto> { Status = false, Message = e.Message };
        }
    }
    public async Task<ApiResponse<IEnumerable<VendorDto>>> GetTop5ByKeywordAsync(string? keyword)
    {
        try
        {
            var models = await _repository.GetTop5VendorsByKeywordAsync(keyword);

            var dtos = await _mapper.ProjectTo<VendorDto>(models).ToListAsync();

            return new ApiResponse<IEnumerable<VendorDto>> { Status = true, Result = dtos };
        }
        catch (Exception e)
        {
            return new ApiResponse<IEnumerable<VendorDto>> { Status = false, Message = e.Message };
        }

    }
    #endregion
}